/* ========================================================================
 * Copyright 2007 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless reqired by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================================
 * Created on 1 oct. 2007
 * @author pollux
 */
package org.poxd.frisar.model;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author pollux
 */
public class DefaultFriseModel implements FriseModel, Serializable {

    private Map<ElementId, FriseElement> model;

    public DefaultFriseModel()
    {
        model = new TreeMap<ElementId, FriseElement>();
    }

    public FriseElement getElement(ElementId id)
    {
        if (id == null)
        {
            throw new IllegalArgumentException("id cannot be null");
        }

        return model.get(id);
    }

    public Iterator<ElementId> keyIterator()
    {
        return model.keySet().iterator();
    }

    public void remove(ElementId id)
    {
        if (id == null)
        {
            throw new IllegalArgumentException("id cannot be null");
        }

        model.remove(id);
    }

    public ElementId addElement(FriseElement element)
    {
        if (element == null)
        {
            throw new IllegalArgumentException("element argument cannot be null");
        }
        Element type = null;
        if (element instanceof Period)
        {
            type = Element.PERIOD;
        }
        else if (element instanceof Event)
        {
            type = Element.EVENT;
        }
        else
        {
            throw new IllegalStateException("You have to override addElement when you create a custom FriseElement");
        }

        ElementId id = new ElementId(type, element.getStart());
        model.put(id, element);
        return id;
    }

    
    public boolean isNextHole(ElementId id)
    {
        if (!model.containsKey(id))
        {
            return false;
        }
        
        for(ElementId key : model.keySet())
        {
            if(key.equals(id))
            {
                return true;
            }
        }
        
        return false;
    }
}
